package com.x.provider.oss.service.impl;

import com.x.provider.oss.service.OssService;
import com.x.provider.oss.service.TencentOssService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OssServiceImpl implements OssService {

    private final String HTTP_PREFIX = "http";

    private final TencentOssService tencentOssService;

    public OssServiceImpl(TencentOssService tencentOssService){
        this.tencentOssService = tencentOssService;
    }

    @Override
    public String getObjectBrowseUrl(String objectKey) {
        return tencentOssService.getObjectBrowseUrl(objectKey);
    }

    @Override
    public Map<String, String> listObjectBrowseUrl(List<String> objectKeys) {
        final Map<String, String> alreadyUrl = objectKeys.stream().filter(item -> item.startsWith(HTTP_PREFIX)).collect(Collectors.toMap(item -> item, item -> item, (s1, s2) -> s1));
        if (alreadyUrl.size() == objectKeys.size()){
            return alreadyUrl;
        }
        alreadyUrl.putAll(tencentOssService.listObjectBrowseUrl(objectKeys.stream().filter(item -> !alreadyUrl.containsKey(item)).collect(Collectors.toList())));
        return alreadyUrl;
    }
}
